//
//  TestViewController.m
//  DoingPaper
//
//  Created by zhuxuhong on 2016/10/18.
//  Copyright © 2016年 zhuxuhong. All rights reserved.
//

#import "TestViewController.h"
#import "PaperDataManager.h"
#import "PaperParam.h"
#import "PaperModel.h"
#import "DoingPaperMainController.h"
#import "CollectedPaperListController.h"

@interface TestViewController ()

@end

@implementation TestViewController
{
    PaperParam *param;
    PaperModel *paper;
    DoingPaperMainController *dvc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    param = [PaperParam new];
    param.paperId = @"29013";//@"28988";//@"39380";
    param.userId = @"1790954";
    param.loginName = @"zxh123";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGRect frame = CGRectMake(40, 50, 100, 40);
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn1 setTitle:@"下载试题" forState:UIControlStateNormal];
    btn1.frame = frame;
    btn1.tag = 100;
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn2 setTitle:@"交卷" forState:UIControlStateNormal];
    frame.origin.y += 50;
    btn2.frame = frame;
    btn2.tag = 200;
    
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn3 setTitle:@"收藏" forState:UIControlStateNormal];
    frame.origin.y += 50;
    btn3.frame = frame;
    btn3.tag = 300;
    
    UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn4 setTitle:@"我的收藏" forState:UIControlStateNormal];
    frame.origin.y += 50;
    btn4.frame = frame;
    btn4.tag = 400;
    
    UIButton *btn5 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn5 setTitle:@"做题" forState:UIControlStateNormal];
    frame.origin.y += 150;
    btn5.frame = frame;
    btn5.tag = 500;
    
    [btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn2 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn3 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn4 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn5 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn1];
    [self.view addSubview:btn2];
    [self.view addSubview:btn3];
    [self.view addSubview:btn4];
    
    [self.view addSubview:btn5];
    
}

-(void)btnClicked: (UIButton*)sender{
    switch (sender.tag) {
        case 100: //下载试题
        {
            [[PaperDataManager sharedManager] getPaperWithParam:param succeed:^(PaperModel *p) {
                NSLog(@"试题下载解析成功: %@",p);
                paper = p;
            } finished:^{
                
            } failed:^(NSError *error) {
                NSLog(@"%@: ",error.domain);
            }];
            break;
        }
        case 200: //交卷
        {
            [[PaperDataManager sharedManager] submitPaperAnwserWithPaper:paper PaperParam:param succeed:^{
                NSLog(@"交卷成功");
            } finished:^{
                
            } failed:^(NSError *error) {
                NSLog(@"%@: ",error.domain);
            }];
            break;
         }
            
        case 300: // 收藏,取消收藏
        {
            PaperItem *item = paper.items[0];
            PaperQuestion *q = item.questions[1];
            if (!q.collected) {
                [[PaperDataManager sharedManager] collectPaperNewWithQuestion:q paperParam:param succeed:^{
                    q.collected = true;
                    NSLog(@"收藏试题成功");
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [sender setTitle:@"取消收藏" forState:UIControlStateNormal];
                    });
                } failed:^(NSError *error) {
                    NSLog(@"%@: ",error.domain);
                }];
            }
            else{
                [[PaperDataManager sharedManager] cancelCollectPaperNewWithQuestion:q paperParam:param succeed:^{
                    q.collected = false;
                    NSLog(@"取消收藏试题成功");
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [sender setTitle:@"收藏" forState:UIControlStateNormal];
                    });
                } failed:^(NSError *error) {
                    NSLog(@"%@: ",error.domain);
                }];
            }
            break;
        }
            
        case 400: //收藏列表
        {
            CollectedPaperController *listVC = [[CollectedPaperController alloc] initWithPaperParam:@{@"userId": @"1790954", @"loginName": @"zxh123"}];
            [self presentViewController:listVC animated:true completion:nil];
            break;
        }
        case 500: //做题
        {
            dvc = [[DoingPaperMainController alloc]initWithParams:@{
              @"userId": @"1790954",
              @"loginName": @"zxh123",
              @"paperId": @"29013",//@"7217"
              @"submited": @"0"
            } submitPaperCompletion:nil];
            [self presentViewController:dvc animated:true completion:nil];
            break;
        }
        default:
            break;
    }
}


@end
